﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;
namespace NetCoreCMS.Web.Areas.Admin.Controllers
{

    public partial class Sys_ConfigController : ManageController
    {
        string defaultpassword = "0|0|0|0"; //默认显示密码
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            base.OnActionExecuting(context);
            if (context.Result != null)
                return;
            ChkAdminLevel("sys_config", CMSEnums.ActionEnum.View.ToString()); //检查权限
        }
        // GET: /Sys_Config/
        public ActionResult Index()
        {
            ShowInfo();
            return View("~/Areas/Admin/Views/settings/sys_config.cshtml");
        }

        #region 赋值操作=================================
        private void ShowInfo()
        {
            var model = sysConfig;
            ViewData["model"] = model;
            ViewBag.labSmsCount = GetSmsCount(); //取得短信数量
        }
        #endregion

        #region 获取短信数量=================================
        private string GetSmsCount()
        {
            string code = string.Empty;
            int count = 0;
            if (code == "115")
            {
                return "查询出错：请完善账户信息";
            }
            else if (code != "100")
            {
                return "错误代码：" + code;
            }
            return count + " 条";
        }
        #endregion

        [HttpPost]
        /// <summary>
        /// 保存配置信息
        /// </summary>
        public ActionResult SubmitSave(string webcountcode, string webclosereason)
        {
            string result = string.Empty;
            ChkAdminLevel("sys_config", CMSEnums.ActionEnum.Edit.ToString()); //检查权限
            var model = sysConfig;
            try
            {
                model.webname = Request.Form["webname"];
                model.weburl = Request.Form["weburl"];
                model.webcompany = Request.Form["webcompany"];
                model.webaddress = Request.Form["webaddress"];
                model.webtel = Request.Form["webtel"];
                model.webfax = Request.Form["webfax"];
                model.webmail = Request.Form["webmail"];
                model.webcrod = Request.Form["webcrod"];

                model.memberstatus = Request.Form["memberstatus"].FirstOrDefault().ToLower().IndexOf("true") >= 0 ? 1 : 0;
                model.commentstatus = Request.Form["commentstatus"].FirstOrDefault().ToLower().IndexOf("true") >= 0 ? 1 : 0;
                model.logstatus = Request.Form["logstatus"].FirstOrDefault().ToLower().IndexOf("true") >= 0 ? 1 : 0;
                model.webstatus = Request.Form["webstatus"].FirstOrDefault().ToLower().IndexOf("true") >= 0 ? 1 : 0;

                model.webclosereason = webclosereason;
                model.webcountcode = webcountcode;

                model.smssignname = Request.Form["smssignname"];
                model.smsusername = Request.Form["smsusername"];
                model.smspassword = Request.Form["smspassword"];

                model.emailsmtp = Request.Form["emailsmtp"];

                model.emailssl = Request.Form["emailssl"].FirstOrDefault().ToLower().IndexOf("true") >= 0 ? 1 : 0;

                model.emailport = Utils.StrToInt(Request.Form["emailport"], 25);
                model.emailfrom = Request.Form["emailfrom"];
                model.emailusername = Request.Form["emailusername"];
                model.emailpassword = Request.Form["emailpassword"];
                model.emailnickname = Request.Form["emailnickname"];

                model.filepath = Request.Form["filepath"];
                model.filesave = Utils.StrToInt(Request.Form["filesave"], 2);
                model.fileextension = Request.Form["fileextension"];
                model.videoextension = Request.Form["videoextension"];
                model.attachsize = Utils.StrToInt(Request.Form["attachsize"], 0);
                model.videosize = Utils.StrToInt(Request.Form["videosize"], 0);
                model.imgsize = Utils.StrToInt(Request.Form["imgsize"], 0);
                model.imgmaxheight = Utils.StrToInt(Request.Form["imgmaxheight"], 0);
                model.imgmaxwidth = Utils.StrToInt(Request.Form["imgmaxwidth"], 0);
                model.thumbnailheight = Utils.StrToInt(Request.Form["thumbnailheight"], 0);
                model.thumbnailwidth = Utils.StrToInt(Request.Form["thumbnailwidth"], 0);
                model.thumbnailmode = Request.Form["thumbnailmode"];
                model.watermarktype = Utils.StrToInt(Request.Form["watermarktype"], 0);
                model.watermarkposition = Utils.StrToInt(Request.Form["watermarkposition"], 9);
                model.watermarkimgquality = Utils.StrToInt(Request.Form["watermarkimgquality"], 80);
                model.watermarkpic = Request.Form["watermarkpic"];
                model.watermarktransparency = Utils.StrToInt(Request.Form["watermarktransparency"], 5);
                model.watermarktext = Request.Form["watermarktext"];
                model.watermarkfont = Request.Form["watermarkfont"];
                model.watermarkfontsize = Utils.StrToInt(Request.Form["watermarkfontsize"], 12);

                model.fileserver = Request.Form["fileserver"];
                model.osssecretid = Request.Form["osssecretid"].FirstOrDefault().Trim();
                model.osssecretkey = Request.Form["osssecretkey"].FirstOrDefault().Trim();
                model.ossbucket = Request.Form["ossbucket"].FirstOrDefault().Trim();
                model.ossendpoint = Request.Form["ossendpoint"].FirstOrDefault().Trim();
                model.ossdomain = Request.Form["ossdomain"].FirstOrDefault().Trim();
                Common.XMLConfig.Sysconfig.saveConifg(model,_env.ContentRootPath);
                AddAdminLog(CMSEnums.ActionEnum.Edit.ToString(), "修改系统配置信息"); //记录日志
                result = JscriptMsg("修改系统配置成功！", "index");
            }
            catch
            {
                result = JscriptMsg("文件写入失败，请检查文件夹权限！", "back");
            }
            ViewBag.ClientScript = result;
            return View("~/Areas/Admin/Views/settings/sys_config.cshtml");
        }
    }
}
